/**
 * Created by Administrator on 2017/1/13.
 */

function RoomManager() {
    this.rooms = {};            // 房间列表 {rid: room}
}


RoomManager.prototype = {

    getRoom: function(rid) {
        return this.rooms[rid];
    },

    addRoom: function(room) {
        this.rooms[room.rid()] = room;
    },

    getRoomCount: function() {
        return Object.keys(this.rooms).length;
    },

    registerTimer: function() {
        setInterval(function() {
            var nowStamp = Date.getStamp();
            var destroyedRids = [];
            ExObject.each(this.rooms, function(rid, room) {
                if (room.isDestroyed()) {
                    if (nowStamp > room.getDestroyTime() + 60) {
                        destroyedRids.push(+rid);
                    }
                } else {
                    room.tick();
                }
            });

            for (var i = 0, l = destroyedRids.length; i < l; ++i) {
                delete this.rooms[destroyedRids[i]];
            }
        }.bind(this), 1000);
    },
}

module.exports = new RoomManager();